package com.qf.topic;

import com.qf.util.MQConnUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Publisher {

    public static final String EXCHANGE_NAME = "topic_exchange";
    public static final String QUEUE_NAME1 = "topic_queue1";
    public static final String QUEUE_NAME2 = "topic_queue2";
    public static final String ROUTING_KEY1 = "*.orange.*";
    public static final String ROUTING_KEY2 = "*.*.rabbit";
    public static final String ROUTING_KEY3 = "lazy.#";


    @Test
    public void publish() throws Exception, TimeoutException {
        //1. conn
        Connection conn = MQConnUtil.getConn();
        //2. channel
        Channel channel = conn.createChannel();
        //3. exchange
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        //4. queue
        channel.queueDeclare(QUEUE_NAME1,true,false,false,null);
        channel.queueDeclare(QUEUE_NAME2,true,false,false,null);
        //5. binding
        channel.queueBind(QUEUE_NAME1,EXCHANGE_NAME,ROUTING_KEY1);
        channel.queueBind(QUEUE_NAME2,EXCHANGE_NAME,ROUTING_KEY2);
        channel.queueBind(QUEUE_NAME2,EXCHANGE_NAME,ROUTING_KEY3);
        // 消息中如果携带的RoutingKey是  什么.orange.什么,会被路由到队列1
        //消息中如果携带的RoutingKey是  什么.什么.rabbit,或者lazy.什么.什么.什么...... 会被路由到队列2
        //6. send
        channel.basicPublish(EXCHANGE_NAME,"big.orange.rabbit",null,"大橙兔!".getBytes());  // queue1,queue2
        channel.basicPublish(EXCHANGE_NAME,"big.white.rabbit",null,"大白兔!".getBytes());  // queue2
        channel.basicPublish(EXCHANGE_NAME,"lazy.rabbit.orange",null,"懒兔橙！".getBytes()); // queue2
        //7. close
        channel.close();
        conn.close();
    }


}
